import defaultSetting from "../../common/utils/defaultSetting";
import utils from "../../common/utils/utils";
import routes from "../../router/router.config";
import store from "@/store"

const user = {
  state: {
    mainAppProps: {}, // 主应用传递过来的数据
    token: "", // token信息
    lang: "zh_CN", // 当前语言
    buildingInfo: {}, // 楼宇选中情况
    menuList: [], // 菜单表
    menuNameList: [], // 菜单alias表
    appHeight: 0,
    currentRoute: {}, // 当前路由
    currentActions: {}, // 当前路由的按钮权限
    userInfo: {}, // 用户信息
    orgInfo: {}, // 机构信息
    appMenuList: {}, //有左侧菜单时传给子应用的需要展示的菜单
    microShareDataName: "", // 主子应用共享的变量名
    microShareData: {}, // 主子应用共享的变量数据
    iframeData: {},
    tableConfig:{},
    watermarkStr:"",
  },
  mutations: {
    SET_MAINAPP_PROPS: (state, props) => {
      state.mainAppProps = props;
    },
    SET_BUILDING_INFO: (state, buildingInfo) => {
      state.buildingInfo = { ...state.buildingInfo, ...buildingInfo };
    },
    SET_USER_INFO: (state, userInfo) => {
      state.userInfo = userInfo;
    },
    SET_ORG_INFO: (state, orgInfo) => {
      state.orgInfo = orgInfo;
    },
    SET_TOKEN: (state, token) => {
      state.token = token;
    },
    SET_LANG: (state, lang) => {
      state.lang = lang;
    },
    SET_MENU: (state, { menuList, nameList }) => {
      state.menuList = menuList;
      state.menuNameList = nameList;
    },
    SET_APP_HEIGHT: (state, height) => {
      state.appHeight = height;
    },
    SET_CURRENT_MENU: (state, menu) => {
      state.currentRoute = menu;
      state.currentActions = menu.action;
    },
    SET_SHARE_DATA: (state, data) => {
      state.microShareDataName = data.name;
      state.microShareData = data.data;
    },
    SET_IFRAME_DATA: (state, data) => {
      state.iframeData = { ...data };
    },
    SET_TABLE_CONFIG: (state, data) => {
      state.tableConfig = data;
    },
    SET_WATERMARK_STR: (state, str) => {
      state.watermarkStr = str;
    },
  },
  actions: {
    /**
     * @description: 存储应用通信数据
     * @param {*} commit
     * @param {*} props
     */
    SetMainAppProps({ commit }, props) {
      return new Promise((resolve) => {
        commit("SET_MAINAPP_PROPS", props);
        commit("SET_USER_INFO", props.userInfo);
        commit("SET_ORG_INFO", props.orgInfo);
        commit("SET_BUILDING_INFO", props.buildingInfo);
        commit("SET_LANG", props.lang);
        commit("SET_TABLE_CONFIG", props.tableConfig);
        let watermarkStr = `${props.userInfo.realname}@${
          props.orgInfo.org_short_name
            ? props.orgInfo.org_short_name
            : props.orgInfo.org_name
        } ${props.userInfo.phone_suffix ?? ''}`;
        commit("SET_WATERMARK_STR",watermarkStr)
        let tableConfig = store.getters.tableConfig;
        if (tableConfig?.isZebra) {
          document.documentElement.style.setProperty(
            "--zebraColor",
            tableConfig.zebraColor
          );
        } else {
          document.documentElement.style.setProperty("--zebraColor", "#fff");
        }
        document.documentElement.style.setProperty(
          "--tableContentSize",
          tableConfig.tableContentSize
        );
        document.documentElement.style.setProperty(
          "--tableTitleSize",
          tableConfig.tableTitleSize
        );
        document.documentElement.style.setProperty(
          "--tableHeaderSize",
          tableConfig.tableHeaderSize
        );

        const { appMenu } = props;
        if (
          appMenu &&
          Object.keys(appMenu).length &&
          appMenu[defaultSetting.name] &&
          appMenu[defaultSetting.name].length
        ) {
          const menuList = appMenu[defaultSetting.name];
          let nameList = [];
          menuList.forEach((item) => {
            if (!nameList.includes(item.alias)) {
              nameList.push(item.alias);
            }
          });
          commit("SET_MENU", { menuList, nameList });
          // utils.consoleInfo(menuList, "menuList");
          // utils.consoleInfo(nameList, "nameList");
        }
        resolve(props);
      });
    },
    /**
     * @description: 设置token
     * @param {*}token
     */
    SetToken({ commit }, token) {
      return new Promise((resolve) => {
        commit("SET_TOKEN", token);
        resolve();
      });
    },
    /**
     * @description: 设置app高度
     * @param {*}token
     */
    SetAppHeight({ commit }, height) {
      commit("SET_APP_HEIGHT", height);
    },
    SetCurrentMenu({ commit }, menu) {
      commit("SET_CURRENT_MENU", menu);
    },
    SetSession({ commit }, name) {
      return new Promise((resolve) => {
        if (window[name]) {
          window[name][`${defaultSetting.name}_routes`] = routes;
        }
        const data = window[name] || {};
        utils.consoleInfo(window[name], "SetSession");
        commit("SET_SHARE_DATA", { name, data });
        resolve();
      });
    },
    SetIframeData({ commit, dispatch }, info) {
      return new Promise((resolve) => {
        const { token, buildingInfo, userInfo } = info;
        if (token) {
          dispatch("SetToken", token);
        }
        if (buildingInfo) {
          commit("SET_BUILDING_INFO", JSON.parse(buildingInfo));
        }
        if (userInfo) {
          commit("SET_USER_INFO", JSON.parse(userInfo));
        }
        commit("SET_IFRAME_DATA", info);
        resolve();
      });
    },
  },
};

export default user;
